﻿using System;
using System.Collections.Generic;
using System.Text;
using B4ALL.Cache.Singleton.Contract;

using Spring.Context;
using Spring.Context.Support;
using B4ALL.JITL;

namespace B4ALL.Cache.Singleton
{
    [Serializable()]
    public class CacheSingleton : JITLObject, ICacheSingleton
    {

        private CacheSingletonReference reference = null;

        #region ICacheSingleton Members
        
        public void Load(Object obj)
        {
            IApplicationContext l_ctx = ContextRegistry.GetContext();
            ICacheSingletonService cacheSingletonService = (ICacheSingletonService)l_ctx.GetObject("cacheSingletonService");

            //TODO DISABLE CACHE

            cacheSingletonService.LoadByRef(obj);
        }

        public CacheSingletonReference Reference
        {
            get
            {
                return reference;
            }
            set
            {
                base.Reference = value;
                reference = value;
            }
        }

        #endregion

        #region Boolean operators overload
        public static Boolean operator ==(CacheSingleton obj1, CacheSingleton obj2)
        {
            if (obj1 == null && obj2 == null)
                return true;
            else if ((obj1 == null && obj2.IsNull) || (obj1.IsNull && obj2 == null))
                return true;
            else if ((obj1 == null && !obj2.IsNull) || (!obj1.IsNull && obj2 == null))
                return false;
            else return obj1.Equals(obj2);
        }

        public static Boolean operator !=(CacheSingleton obj1, CacheSingleton obj2)
        {
            if (obj1 == null && obj2 == null)
                return false;
            else if ((obj1 == null && obj2.IsNull) || (obj1.IsNull && obj2 == null))
                return false;
            else if ((obj1 == null && !obj2.IsNull) || (!obj1.IsNull && obj2 == null))
                return true;
            else return !obj1.Equals(obj2);
        }
        #endregion Boolean operators overload

    }
}